package com.ebt.m.customer.view;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ebt.m.R;

import java.util.ArrayList;

/**
 * Created by Rick.Wang on 2016/4/19.
 */
public class CustomerHeaderViewGroup extends LinearLayout {

    private ViewPager header_view_pager;
    private HalfWhiteBgTextView header_text_view;

    private HeaderPagerAdapter adapter;

    public CustomerHeaderViewGroup(Context context) {
        this(context, null);
    }

    public CustomerHeaderViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomerHeaderViewGroup(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View contentView = inflate(context, R.layout.view_customer_header_view_group, this);
        header_view_pager = (ViewPager)contentView.findViewById(R.id.header_view_pager);
        header_text_view = (HalfWhiteBgTextView)contentView.findViewById(R.id.header_text_view);
    }

    public void setTextViewContent(String text){
        header_text_view.setText(text);
    }

    public void setTextViewVisibility(int visibility){
        header_text_view.setVisibility(visibility);
    }

    public void setViewPagerCount(int count){
        if(count <= 0){
            count = 1;
        }
        adapter = new HeaderPagerAdapter(count);
        header_view_pager.setAdapter(adapter);
        header_view_pager.setCurrentItem(count - 1, false);
    }

    private class HeaderPagerAdapter extends PagerAdapter {

        private ArrayList<CustomerHeaderViewPagerItem> pages = new ArrayList<CustomerHeaderViewPagerItem>();

        private int count = 0;
        public HeaderPagerAdapter(int count) {
            this.count = count;
            for(int i = 0; i < count; i++) {
                pages.add(null);
            }
        }

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            CustomerHeaderViewPagerItem pageView = pages.get(position);
            if(pageView == null){
                pageView = new CustomerHeaderViewPagerItem(getContext());
                pages.set(position, pageView);
            }

            if (pageView != null) {
                container.addView(pageView);
                pageView.update(count - 1 - position);
            }
            return pageView;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }
    }

}